<?php
namespace Home\Controller;
use Think\Controller;
class ActivityController extends Controller {
   public function activitylist(){
        $activityModel=M('activity');
        $data=$activityModel->order('aid')->select();
        // var_dump($data);
         $this->assign('activitylist',$data);
       $this->display();
    }
    public function activitydetail(){
      $id=$_GET['aid'];
        $activityModel=M('activity');
        $data=$activityModel->where("aid='$id'")->find();
        // var_dump($data);
         $this->assign('activitydetail',$data);
       $this->display();
    }

public function delactivity( )
    {

       // $id=I('id');
        $id=$_GET['aid'];
        $activityModel = M('activity');//实例化news对象
        if($activityModel->delete($id)) {
            $this->success('删除成功');
            //echo json_encode('1');
        }
        else {
           $this->error('删除失败');
          
            //echo json_encode('0');
        }
    }

    public function editactivity($aid)
    {
       
    $activityModel=M('activity');
    $data=$activityModel->where("aid=$aid")->find();
    $this->activitydetail=$data;
    $this->display();
    //var_dump($data);
    // echo json_encode($one);
   }

   public function updateactivity(){

            $data=$_POST;
            $file=$_FILES;
            $aid=$data['aid'];
            //dump($data);
           // dump($file);
                    
            $activityModel = M('activity'); // 实例化User模型
            // 要修改的数据对象属性赋值
            //$userModel->create();   // 根据表单提交的POST数据创建数据对象
            //dump($data);
        //$result=$userModel->where("id=$id")->save($data);

        if($file['apost']['size']==0) {
            //没有修改头像
            $result=$activityModel->where("aid=$aid")->save($data);
  
        }else{
            //修改头像
            $upload = new \Think\Upload();// 实例化上传类
            $upload->maxSize  = 3145728 ;// 设置附件上传大小
            $upload->allowExts  = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
            $upload->rootPath  =  './';
            $upload->savePath =  'Public/Uploads/picture/';// 设置附件上传目录
            $upload->autoSub = false;
            $upload->saveName = date("YmdHis").rand(100,999);//定义名字
            $info = $upload->upload();
    
            if(!$info) {// 上传错误提示错误信息
                    $this->error($upload->getError());
             }else{// 上传成功 获取上传文件信息
                 //保存表单数据 包括附件数据
                $data['apost']= $info['apost']['savename']; 
                $result=$activityModel->where("aid=$aid")->save($data);
                    if ($data['oldpic']!='nopic.jpg') {
                    $oldpicPath='Public/Uploads/picture/'.$data['oldpic'];
                    unlink($oldpicPath);
                        }
                    }
            }
        if($result){
             $this->success('修改成功！','activitylist');
             }
            else{
                $this->error('修改失败！');
                }
    }






    public function insertactivity(){
        $data=$_POST;
        $file=$_FILES;
        //dump($file['photo']['size']);
                
        //没有头像
        $activityModel = M('activity'); // 实例化User模型
        $createresult=$activityModel->create();   // 根据表单提交的POST数据创建数据对象
          if ($createresult) {
             
               $activityModel->aname = trim(I('post.aname'));
               
                $activityModel->aenddate = trim(I('post.aenddate'));
                $activityModel->astartdate = trim(I('post.astartdate'));
                $activityModel->address = trim(I('post.aaddress'));
                $activityModel->aphonenum = trim(I('post.aphonenum'));
                $activityModel->asponsor = trim(I('post.asponsor'));

              
       
             if ($file['apost']['size']==0) {//没有头像
                        $apostModel->apost = "nopic.jpg"; // 用户不上传头像默认nopic.jpg
                    }else{//有头像
                            $info=$this->MyUpload();
                            $activityModel->apost = $info['apost']['savename']; // 保存上传的照片根据需要自行组装
                          }
                 $addresult=$activityModel->add(); // 写入用户数据到数据库
                     if (false !== $addresult)
                             {
                            $this->success('添加成功！', 'activitylist');
                             }else{ 
                                     $this->error('数据错误'); 
                                   }

        }else{
                $this->error($activityModel->getError());  
              }


    }
        public function MyUpload(){
                //上传招领图片
              $upload = new \Think\Upload();// 实例化上传类
              $upload->maxSize  = 3145728 ;// 设置附件上传大小
              $upload->allowExts  = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
              $upload->rootPath  =  './';
              $upload->savePath =  'Public/Uploads/picture/';// 设置附件上传目录
              $upload->autoSub = false;
              $upload->saveName = date("YmdHis").rand(100,999);//定义名字
               // 上传单个文件 
              $info = $upload->upload();                    
                  if(!$info) {// 上传错误提示错误信息
                      $this->error($upload->getError());
                    }else{// 上传成功 获取上传文件信息
                return $info;
                         }
    }
}












    
